package com.css.fxfzysx.modules.vaolEngineering.controller;

import cn.hutool.core.io.FileUtil;
import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxRoadEntity;
import com.css.fxfzysx.modules.vaolEngineering.param.update.RoadParam;
import com.css.fxfzysx.modules.vaolEngineering.service.CarVulnerabilityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;

@RestController
@RequestMapping("carVulnerability")
@Slf4j
public class CarVulnerabilityController {

    @Autowired
    private CarVulnerabilityService carVulnerabilityService;

    /**
     * 道路易损性计算过程查看
     * @param id
     * @return
     */
    @GetMapping("getById")
    public RestResponse getById(String id){
        try {
            SmxRoadEntity entity = carVulnerabilityService.getById(id);
            return RestResponse.succeed(entity);
        } catch (Exception e) {
            log.error("get getById error:{}",e.getMessage(),e);
            return RestResponse.fail("查询失败!");
        }
    }

    /**
     * 道路易损性计算和保存
     * @param versionId
     * @return
     */
    @GetMapping("crVulnerability")
    public RestResponse crVulnerability(String versionId,String tableId){
        return carVulnerabilityService.crVulnerability(versionId,tableId);
    }

    /**
     * 道路导入
     * @param file
     * @return
     */
    @PostMapping("importVulnerability")
    public RestResponse importVulnerability(@RequestParam MultipartFile file){
        if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
            return RestResponse.fail("文件格式不支持!");
        }
        return carVulnerabilityService.importVulnerability(file);
    }

    /**
     * 数据验证
     */
    @GetMapping("getDataValidation")
    public RestResponse getDataValidation(String versionId){
        try {
            HashMap<Integer, ArrayList<String>> map = carVulnerabilityService.getDataValidation(versionId);
            return RestResponse.succeed(map);
        } catch (Exception e) {
            log.error("get getDataValidation error:{}",e.getMessage(),e);
            return RestResponse.fail("校验失败!");
        }
    }

    /**
     * 计算
     * @return
     */
    @PostMapping("count")
    public RestResponse count(@RequestBody RoadParam param){
        try {
            return carVulnerabilityService.count(param);
        } catch (Exception e) {
            log.error("post count error:{}",e.getMessage(),e);
            return RestResponse.fail("数据异常,计算失败!");
        }
    }
}
